// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

var UnitTools = require('UnitTools')
var ClickManager = require('ClickManager')
var NetworkManager = require('NetworkManager')

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        createShowBtn: cc.Node, // 创建联盟按钮
        createModel: cc.Node,   // 创建模态框
        createBtn: cc.Node,     // 创建
        unionName: cc.EditBox,  // 创建的联盟名称
        moreModel: cc.Node,     // 更多的模态框
        alertModel: cc.Prefab
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.token = cc.sys.localStorage.getItem('__TOKEN__')

        // 显示创建框
        ClickManager.onClick(this.createShowBtn, this, (arg) => {
            this.moreModel.active = false
            this.createModel.active = true
        })

        // 创建联盟
        ClickManager.onClick(this.createBtn, this, (arg) => {
            this.respCreateUnion()
        })
    },

    start () {

    },

    // 创建联盟
    respCreateUnion () {
        let unionName = this.unionName.string

        try {
            NetworkManager.onConnectedToHall( (hallService) => {
                hallService.proxy.createUnion(this.token, unionName, (data) => {
                    this.createModel.active = false
                    ClickManager.alertModel(this.alertModel, data.msg)
                })
            })
        } catch (err) {
            console.log('创建联盟异常处理，待处理:', err)
            cc.director.loadScene('loading')
        }
    }

    // update (dt) {},
});
